package com.adams.action.view;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.beanutils.BeanMap;
import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.apache.log4j.Logger;

import com.adams.access.db.bean.Menu;
import com.adams.access.db.bean.ViewEvent;
import com.adams.access.db.bean.ViewItem;
import com.adams.access.db.bean.ViewPanel;
import com.adams.access.db.bean.ViewPanelFilter;
import com.adams.access.db.bean.ViewWin;
import com.adams.action.view.ViewAction;
import com.adams.custom.page.mgt.service.ViewCfgService;
import com.adams.custom.page.util.ViewConstant;
import com.adams.custom.page.util.ViewUtil;
import com.adams.sys.menu.service.MenuService;

/**
 * 
 * File Name : ViewCfgAction.java
 * 
 * @Description : 界面组件配置action类
 * @author 904370
 */
public class ViewCfgAction extends ViewAction
{

	private static final long serialVersionUID = 7652146782094447813L;

	Logger logger = Logger.getLogger(ViewCfgAction.class.getName());

	/**
	 * Description : 菜单服务类 Author : 904370
	 */
	private MenuService menuService;
	/**
	 * Description : 界面配置服务类 Author : 904370
	 */
	private ViewCfgService viewCfgService;

	private long id;

	public void setId(long id)
	{
		this.id = id;
	}

	public void setMenuService(MenuService menuService)
	{
		this.menuService = menuService;
	}

	public void setViewCfgService(ViewCfgService viewCfgService)
	{
		this.viewCfgService = viewCfgService;
	}

	/**
	 * Description : 跳转去配置页面
	 * 
	 * @return
	 * @throws Exception
	 * 
	 */
	@SuppressWarnings("unchecked")
	public String turnToCfg() throws Exception
	{
		logger.info("ViewCfgAction turnToCfg method run begin.");
		Map map = viewParseService.getParseViewByMenuId(getUserId(), menuId);
		Object children = map.get("wins");
		Map winMap = new HashMap();
		winMap.put(ViewConstant.JSON_ID, "propertyconfig-win");
		winMap.put("xtype", "propertyconfig-win");
		winMap.put("x", "0");
		winMap.put("y", "0");
		winMap.put("width", "100%");
		winMap.put("height", "100%");
		winMap.put("children", children);

		List<Map> winsMap = new ArrayList<Map>();
		winsMap.add(winMap);

		Map viewMap = new HashMap();
		viewMap.put("wins", winsMap);
		write(JSONObject.fromObject(viewMap).toString());
		logger.info("ViewCfgAction turnToCfg method run end.");
		return null;
	}

	/**
	 * Description : 菜单列表
	 * 
	 * @return
	 * @throws Exception
	 * 
	 */
	@SuppressWarnings("unchecked")
	public String findMenu() throws Exception
	{
		logger.info("ViewCfgAction findMenu method run begin.");
		if (ViewUtil.isNuLLStr(gridPanelId))
		{
			gridPanelId = request.getParameter("gridPanelId");
		}
		List<Menu> viewGridDatasList = menuService.queryAllSubMenus();
		Collections.sort(viewGridDatasList, new Comparator<Menu>()
		{
			@Override
			public int compare(Menu o1, Menu o2)
			{
				// TODO Auto-generated method stub
				return o1.getId().compareTo(o2.getId());
			}
		});
		Map viewGridMap = viewParseService.getParseGridDatasFromList(
				getUserId(), gridPanelId, viewGridDatasList, viewGridDatasList
						.size());
		write(JSONObject.fromObject(viewGridMap).toString());
		logger.info("ViewCfgAction findMenu method run end.");
		return null;
	}

	/**
	 * Description : 界面窗口列表
	 * 
	 * @return
	 * @throws Exception
	 * 
	 */
	@SuppressWarnings("unchecked")
	public String findViewWin() throws Exception
	{
		logger.info("ViewCfgAction findViewWin method run begin.");
		if (ViewUtil.isNuLLStr(gridPanelId))
		{
			gridPanelId = request.getParameter("gridPanelId");
		}
		List<ViewWin> viewGridDatasList = viewCfgService
				.findViewWinByMenuid(id);
		List<Map> columnMaps = ViewUtil
				.Objects2ColumnMapsBy_C(viewGridDatasList);
		Map viewGridMap = viewParseService.getParseGridDatasFromList(
				getUserId(), gridPanelId, columnMaps, columnMaps.size());
		write(JSONObject.fromObject(viewGridMap).toString());
		logger.info("ViewCfgAction findViewWin method run end.");
		return null;
	}

	/**
	 * Description : 跳转新增界面窗口
	 * 
	 * @return
	 * @throws Exception
	 * 
	 */
	@SuppressWarnings("unchecked")
	public String getSaveViewWinForm() throws Exception
	{
		logger.info("ViewCfgAction getSaveViewWinForm method run begin.");
		if (ViewUtil.isNuLLStr(viewEventId))
		{
			viewEventId = request.getParameter("viewEventId");
		}
		List<Map> mapList = ViewAction.getDataMap(request);
		Map inputMap = null;
		if (mapList.size() > 0)
		{
			inputMap = ViewAction.getDataMap(request).get(0);
		}
		Map map = viewParseService.getParseViewByEventId(getUserId(), menuId,
				viewEventId, inputMap);
		String action = (String) map.get(ViewConstant.JSON_ACTION);
		action = ViewUtil.addParameterToUrl(action, "menuid", id + "");
		map.put(ViewConstant.JSON_ACTION, action);
		write(JSONObject.fromObject(map).toString());
		logger.info("ViewCfgAction getSaveViewWinForm method run end.");
		return null;
	}

	/**
	 * Description : 新增界面窗口
	 * 
	 * @return
	 * @throws Exception
	 * 
	 */
	@SuppressWarnings("unchecked")
	public String saveOrUpdateViewWin() throws Exception
	{
		logger.info("ViewCfgAction saveOrUpdateViewWin method run begin.");
		Map inputMap = ViewAction.requestParameterMap2Map(request);
		Map objectMap = ViewUtil.ColumnMap2ObjectMapBy_C(inputMap);
		ViewWin viewWin = (ViewWin) ViewUtil.map2Object(objectMap,
				ViewWin.class);
		boolean saveFlag = true;
		if (null != viewWin.getId() && viewWin.getId() > 0)
		{
			saveFlag = false;
		}
		viewCfgService.saveOrUpdateViewWin(viewWin);
		if (saveFlag)
		{
			createLog("addViewWin", "ViewWin id:" + viewWin.getId());
		} else
		{
			createLog("updateViewWin", "ViewWin id:" + viewWin.getId());
		}
		Map result = new HashMap();
		result.put(ViewConstant.JSON_DEAL_SUCESS, true);
		write(JSONObject.fromObject(result).toString());
		logger.info("ViewCfgAction saveOrUpdateViewWin method run end.");
		return null;
	}

	/**
	 * Description : 删除界面窗口
	 * 
	 * @return
	 * @throws Exception
	 * 
	 */
	@SuppressWarnings("unchecked")
	public String deleteViewWin() throws Exception
	{
		logger.info("ViewCfgAction deleteViewWin method run begin.");
		List<Map> mapList = ViewAction.getDataMap(request);
		List<Long> viewwinids = new ArrayList<Long>();
		for (Map map : mapList)
		{
			Object id = map.get(ViewConstant.JSON_ID);
			if (null != id && !"".equals(id.toString())
					&& id.toString().matches("\\d+"))
			{
				viewwinids.add(Long.valueOf(id.toString()));
			}
		}
		viewCfgService.deleteViewWins(viewwinids);
		createLog("deleteViewWin", "ViewWin id:"
				+ ReflectionToStringBuilder.toString(viewwinids));
		Map result = new HashMap();
		result.put(ViewConstant.JSON_DEAL_SUCESS, true);
		write(JSONObject.fromObject(result).toString());
		logger.info("ViewCfgAction deleteViewWin method run end.");
		return null;
	}

	/**
	 * Description : 界面面板列表
	 * 
	 * @return
	 * @throws Exception
	 * 
	 */
	@SuppressWarnings("unchecked")
	public String findViewPanel() throws Exception
	{
		logger.info("ViewCfgAction findViewPanel method run begin.");
		if (ViewUtil.isNuLLStr(gridPanelId))
		{
			gridPanelId = request.getParameter("gridPanelId");
		}
		List<ViewPanel> viewGridDatasList = viewCfgService
				.findViewPanelByViewwinid(id);
		List<Map> columnMaps = ViewUtil
				.Objects2ColumnMapsBy_C(viewGridDatasList);
		Map viewGridMap = viewParseService.getParseGridDatasFromList(
				getUserId(), gridPanelId, columnMaps, columnMaps.size());
		write(JSONObject.fromObject(viewGridMap).toString());
		logger.info("ViewCfgAction findViewPanel method run end.");
		return null;
	}

	/**
	 * Description : 跳转新增面板窗口
	 * 
	 * @return
	 * @throws Exception
	 * 
	 */
	@SuppressWarnings("unchecked")
	public String getSaveViewPanelForm() throws Exception
	{
		logger.info("ViewCfgAction getSaveViewPanelForm method run begin.");
		if (ViewUtil.isNuLLStr(viewEventId))
		{
			viewEventId = request.getParameter("viewEventId");
		}
		List<Map> mapList = ViewAction.getDataMap(request);
		Map inputMap = null;
		if (mapList.size() > 0)
		{
			inputMap = ViewAction.getDataMap(request).get(0);
		}
		Map map = viewParseService.getParseViewByEventId(getUserId(), menuId,
				viewEventId, inputMap);
		String action = (String) map.get(ViewConstant.JSON_ACTION);
		action = ViewUtil.addParameterToUrl(action, "viewwinid", id + "");
		map.put(ViewConstant.JSON_ACTION, action);
		write(JSONObject.fromObject(map).toString());
		logger.info("ViewCfgAction getSaveViewPanelForm method run end.");
		return null;
	}

	/**
	 * Description : 新增界面面板对象
	 * 
	 * @return
	 * @throws Exception
	 * 
	 */
	@SuppressWarnings("unchecked")
	public String saveOrUpdateViewPanel() throws Exception
	{
		logger.info("ViewCfgAction saveOrUpdateViewPanel method run begin.");
		Map inputMap = ViewAction.requestParameterMap2Map(request);
		Map objectMap = ViewUtil.ColumnMap2ObjectMapBy_C(inputMap);
		ViewPanel viewPanel = (ViewPanel) ViewUtil.map2Object(objectMap,
				ViewPanel.class);
		boolean saveFlag = true;
		if (null != viewPanel.getId() && viewPanel.getId() > 0)
		{
			saveFlag = false;
		}
		viewCfgService.saveOrUpdateViewPanel(viewPanel);
		if (saveFlag)
		{
			createLog("addViewPanel", "ViewPanel id:" + viewPanel.getId());
		} else
		{
			createLog("updateViewPanel", "ViewPanel id:" + viewPanel.getId());
		}
		Map result = new HashMap();
		result.put(ViewConstant.JSON_DEAL_SUCESS, true);
		write(JSONObject.fromObject(result).toString());
		logger.info("ViewCfgAction saveOrUpdateViewPanel method run end.");
		return null;
	}

	/**
	 * Description : 删除界面面板
	 * 
	 * @return
	 * @throws Exception
	 * 
	 */
	@SuppressWarnings("unchecked")
	public String deleteViewPanel() throws Exception
	{
		logger.info("ViewCfgAction deleteViewPanel method run begin.");
		List<Map> mapList = ViewAction.getDataMap(request);
		List<Long> viewPanelids = new ArrayList<Long>();
		for (Map map : mapList)
		{
			Object id = map.get("id");
			if (null != id && !"".equals(id.toString())
					&& id.toString().matches("\\d+"))
			{
				viewPanelids.add(Long.valueOf(id.toString()));
			}
		}
		viewCfgService.deleteViewPanels(viewPanelids);
		createLog("deleteViewPanel", "ViewPanel id:"
				+ ReflectionToStringBuilder.toString(viewPanelids));
		Map result = new HashMap();
		result.put(ViewConstant.JSON_DEAL_SUCESS, true);
		write(JSONObject.fromObject(result).toString());
		logger.info("ViewCfgAction deleteViewPanel method run end.");
		return null;
	}

	/**
	 * Description : 界面事件列表
	 * 
	 * @return
	 * @throws Exception
	 * 
	 */
	@SuppressWarnings("unchecked")
	public String findViewEvent() throws Exception
	{
		logger.info("ViewCfgAction findViewEvent method run begin.");
		if (ViewUtil.isNuLLStr(gridPanelId))
		{
			gridPanelId = request.getParameter("gridPanelId");
		}
		List<ViewEvent> viewGridDatasList = viewCfgService
				.findViewEventByViewpanelid(id);
		List<Map> columnMaps = ViewUtil
				.Objects2ColumnMapsBy_C(viewGridDatasList);
		Map viewGridMap = viewParseService.getParseGridDatasFromList(
				getUserId(), gridPanelId, columnMaps, columnMaps.size());
		write(JSONObject.fromObject(viewGridMap).toString());
		logger.info("ViewCfgAction findViewEvent method run end.");
		return null;
	}

	/**
	 * Description : 跳转新增界面事件窗口
	 * 
	 * @return
	 * @throws Exception
	 * 
	 */
	@SuppressWarnings("unchecked")
	public String getSaveViewEventForm() throws Exception
	{
		logger.info("ViewCfgAction getSaveViewEventForm method run begin.");
		if (ViewUtil.isNuLLStr(viewEventId))
		{
			viewEventId = request.getParameter("viewEventId");
		}
		List<Map> mapList = ViewAction.getDataMap(request);
		Map inputMap = null;
		if (mapList.size() > 0)
		{
			inputMap = ViewAction.getDataMap(request).get(0);
		}
		Map map = viewParseService.getParseViewByEventId(getUserId(), menuId,
				viewEventId, inputMap);
		String action = (String) map.get(ViewConstant.JSON_ACTION);
		action = ViewUtil.addParameterToUrl(action, "viewpanelid", id + "");
		map.put(ViewConstant.JSON_ACTION, action);

		Map fieldSetMap = (Map) map.get(ViewConstant.JSON_FIELDSET);
		if (null != fieldSetMap)
		{
			List<Map> itemMaps = (List<Map>) fieldSetMap
					.get(ViewConstant.JSON_ITEMS);
			if (null != itemMaps && itemMaps.size() > 0)
			{
				for (Map itemMap : itemMaps)
				{
					String viewwinid = (String) itemMap.get("name");
					if ("viewwinid".equals(viewwinid))
					{
						String url = (String) itemMap.get("url");
						url = ViewUtil.addParameterToUrl(url, "viewpanelid", id
								+ "");
						itemMap.put("url", url);
						break;
					}
				}
				fieldSetMap.put(ViewConstant.JSON_ITEMS, itemMaps);
			}
		}
		map.put(ViewConstant.JSON_FIELDSET, fieldSetMap);
		write(JSONObject.fromObject(map).toString());
		logger.info("ViewCfgAction getSaveViewEventForm method run end.");
		return null;
	}

	/**
	 * Description : 跳转更新界面事件窗口
	 * 
	 * @return
	 * @throws Exception
	 * 
	 */
	@SuppressWarnings("unchecked")
	public String getUpdateViewEventForm() throws Exception
	{
		logger.info("ViewCfgAction getUpdateViewEventForm method run begin.");
		if (ViewUtil.isNuLLStr(viewEventId))
		{
			viewEventId = request.getParameter("viewEventId");
		}
		List<Map> mapList = ViewAction.getDataMap(request);
		Map inputMap = null;
		if (mapList.size() > 0)
		{
			inputMap = ViewAction.getDataMap(request).get(0);
		}
		Map map = viewParseService.getParseViewByEventId(getUserId(), menuId,
				viewEventId, inputMap);

		String vieweventid = (String) inputMap.get("id");
		ViewEvent viewEvent = viewCfgService.getViewEventById(Long
				.valueOf(vieweventid));
		Map fieldSetMap = (Map) map.get(ViewConstant.JSON_FIELDSET);
		if (null != fieldSetMap)
		{
			List<Map> itemMaps = (List<Map>) fieldSetMap
					.get(ViewConstant.JSON_ITEMS);
			if (null != itemMaps && itemMaps.size() > 0)
			{
				for (Map itemMap : itemMaps)
				{
					String viewwinid = (String) itemMap.get("name");
					if ("viewwinid".equals(viewwinid))
					{
						String url = (String) itemMap.get("url");
						url = ViewUtil.addParameterToUrl(url, "viewpanelid",
								viewEvent.getViewpanelid() + "");
						itemMap.put("url", url);
						break;
					}
				}
				fieldSetMap.put(ViewConstant.JSON_ITEMS, itemMaps);
			}
		}
		map.put(ViewConstant.JSON_FIELDSET, fieldSetMap);
		write(JSONObject.fromObject(map).toString());
		logger.info("ViewCfgAction getUpdateViewEventForm method run end.");
		return null;
	}

	/**
	 * Description : 获得本菜单下的事件窗口
	 * 
	 * @return
	 * @throws Exception
	 * 
	 */
	@SuppressWarnings("unchecked")
	public String getEventWin() throws Exception
	{
		logger.info("ViewCfgAction getEventWin method run begin.");
		String viewpanelidStr = request.getParameter("viewpanelid");
		List<ViewWin> viewWins = viewCfgService
				.findViewWinByViewPanelIdAndWintype(viewpanelidStr,
						ViewWin.WINTYPE_EVENT);
		Map map = viewParseService.getParseDynamicComboFromList(getUserId(),
				menuId, viewWins);
		write(JSONObject.fromObject(map).toString());
		logger.info("ViewCfgAction getEventWin method run end.");
		return null;
	}

	/**
	 * Description : 新增界面事件
	 * 
	 * @return
	 * @throws Exception
	 * 
	 */
	@SuppressWarnings("unchecked")
	public String saveOrUpdateViewEvent() throws Exception
	{
		logger.info("ViewCfgAction saveOrUpdateViewEvent method run begin.");
		Map inputMap = ViewAction.requestParameterMap2Map(request);
		Map objectMap = ViewUtil.ColumnMap2ObjectMapBy_C(inputMap);
		ViewEvent viewEvent = (ViewEvent) ViewUtil.map2Object(objectMap,
				ViewEvent.class);
		boolean saveFlag = true;
		if (null != viewEvent.getId() && viewEvent.getId() > 0)
		{
			saveFlag = false;
		}
		viewCfgService.saveOrUpdateViewEvent(viewEvent);
		if (saveFlag)
		{
			createLog("addViewEvent", "ViewEvent id:" + viewEvent.getId());
		} else
		{
			createLog("updateViewEvent", "ViewEvent id:" + viewEvent.getId());
		}
		Map result = new HashMap();
		result.put(ViewConstant.JSON_DEAL_SUCESS, true);
		write(JSONObject.fromObject(result).toString());
		logger.info("ViewCfgAction saveOrUpdateViewEvent method run end.");
		return null;
	}

	/**
	 * Description : 删除界面事件
	 * 
	 * @return
	 * @throws Exception
	 * 
	 */
	@SuppressWarnings("unchecked")
	public String deleteViewEvent() throws Exception
	{
		logger.info("ViewCfgAction deleteViewEvent method run begin.");
		List<Map> mapList = ViewAction.getDataMap(request);
		List<Long> viewEventids = new ArrayList<Long>();
		for (Map map : mapList)
		{
			Object id = map.get("id");
			if (null != id && !"".equals(id.toString())
					&& id.toString().matches("\\d+"))
			{
				viewEventids.add(Long.valueOf(id.toString()));
			}
		}
		viewCfgService.deleteViewEvents(viewEventids);
		createLog("deleteViewEvent", "ViewEvent id:"
				+ ReflectionToStringBuilder.toString(viewEventids));
		Map result = new HashMap();
		result.put(ViewConstant.JSON_DEAL_SUCESS, true);
		write(JSONObject.fromObject(result).toString());
		logger.info("ViewCfgAction deleteViewEvent method run end.");
		return null;
	}

	/**
	 * Description : 界面内容项列表
	 * 
	 * @return
	 * @throws Exception
	 * 
	 */
	@SuppressWarnings("unchecked")
	public String findViewItem() throws Exception
	{
		logger.info("ViewCfgAction findViewItem method run begin.");
		if (ViewUtil.isNuLLStr(gridPanelId))
		{
			gridPanelId = request.getParameter("gridPanelId");
		}
		List<ViewItem> viewGridDatasList = viewCfgService
				.findViewItemByViewpanelid(id);
		List<Map> columnMaps = ViewUtil
				.Objects2ColumnMapsBy_C(viewGridDatasList);
		Map viewGridMap = viewParseService.getParseGridDatasFromList(
				getUserId(), gridPanelId, columnMaps, columnMaps.size());
		write(JSONObject.fromObject(viewGridMap).toString());
		logger.info("ViewCfgAction findViewItem method run end.");
		return null;
	}

	/**
	 * Description : 跳转新增内容项窗口
	 * 
	 * @return
	 * @throws Exception
	 * 
	 */
	@SuppressWarnings("unchecked")
	public String getSaveViewItemForm() throws Exception
	{
		logger.info("ViewCfgAction getSaveViewItemForm method run begin.");
		if (ViewUtil.isNuLLStr(viewEventId))
		{
			viewEventId = request.getParameter("viewEventId");
		}
		List<Map> mapList = ViewAction.getDataMap(request);
		Map inputMap = null;
		if (mapList.size() > 0)
		{
			inputMap = ViewAction.getDataMap(request).get(0);
		}
		Map map = viewParseService.getParseViewByEventId(getUserId(), menuId,
				viewEventId, inputMap);
		String action = (String) map.get(ViewConstant.JSON_ACTION);
		action = ViewUtil.addParameterToUrl(action, "viewpanelid", id + "");
		map.put(ViewConstant.JSON_ACTION, action);
		Map fieldSetMap = (Map) map.get(ViewConstant.JSON_FIELDSET);
		List<Map> itemMaps = (List<Map>) fieldSetMap
				.get(ViewConstant.JSON_ITEMS);
		for (Map itemMap : itemMaps)
		{
			String name = (String) itemMap.get("name");
			if ("name".equals(name))
			{
				String url = (String) itemMap.get("url");
				if (null != url && !"".equals(url.trim()))
				{
					url = ViewUtil.addParameterToUrl(url, "viewpanelid", id
							+ "");
					itemMap.put("url", url);
				}
			} else if ("viewwinid".equals(name))
			{
				String url = (String) itemMap.get("url");
				url = ViewUtil.addParameterToUrl(url, "viewpanelid", id + "");
				itemMap.put("url", url);
			}
		}
		fieldSetMap.put(ViewConstant.JSON_ITEMS, itemMaps);
		map.put(ViewConstant.JSON_FIELDSET, fieldSetMap);
		write(JSONObject.fromObject(map).toString());
		logger.info("ViewCfgAction getSaveViewItemForm method run end.");
		return null;
	}

	/**
	 * Description : 跳转更新界面事件窗口
	 * 
	 * @return
	 * @throws Exception
	 * 
	 */
	@SuppressWarnings("unchecked")
	public String getUpdateViewItemForm() throws Exception
	{
		logger.info("ViewCfgAction getUpdateViewItemForm method run begin.");
		if (ViewUtil.isNuLLStr(viewEventId))
		{
			viewEventId = request.getParameter("viewEventId");
		}
		List<Map> mapList = ViewAction.getDataMap(request);
		Map inputMap = null;
		if (mapList.size() > 0)
		{
			inputMap = ViewAction.getDataMap(request).get(0);
		}
		Map map = viewParseService.getParseViewByEventId(getUserId(), menuId,
				viewEventId, inputMap);
		String viewitemid = (String) inputMap.get("id");
		ViewItem viewItem = viewCfgService.getViewItemById(Long
				.valueOf(viewitemid));
		Map fieldSetMap = (Map) map.get(ViewConstant.JSON_FIELDSET);
		if (null != fieldSetMap)
		{
			List<Map> itemMaps = (List<Map>) fieldSetMap
					.get(ViewConstant.JSON_ITEMS);
			if (null != itemMaps && itemMaps.size() > 0)
			{
				for (Map itemMap : itemMaps)
				{
					String name = (String) itemMap.get("name");

					if ("name".equals(name))
					{
						String url = (String) itemMap.get("url");
						if (null != url && !"".equals(url.trim()))
						{
							url = ViewUtil.addParameterToUrl(url,
									"viewpanelid", viewItem.getViewpanelid()
											+ "");
							itemMap.put("url", url);
						}
					} else if ("viewwinid".equals(name))
					{
						String url = (String) itemMap.get("url");
						url = ViewUtil.addParameterToUrl(url, "viewpanelid",
								viewItem.getViewpanelid() + "");
						itemMap.put("url", url);
					}
				}
				fieldSetMap.put(ViewConstant.JSON_ITEMS, itemMaps);
			}
		}
		map.put(ViewConstant.JSON_FIELDSET, fieldSetMap);
		write(JSONObject.fromObject(map).toString());
		logger.info("ViewCfgAction getUpdateViewItemForm method run end.");
		return null;
	}

	/**
	 * Description : 获取列定义选择树
	 * 
	 * @return
	 * @throws Exception
	 * 
	 */
	@SuppressWarnings("unchecked")
	public String getParseColumnCodeTree() throws Exception
	{
		logger.info("ViewCfgAction getParseColumnCodeTree method run begin.");
		String viewpanelidStr = request.getParameter("viewpanelid");
		Long id = ViewUtil.getViewControlId(viewpanelidStr);
		ViewPanel viewPanel = viewCfgService.getViewPanelById(id);
		String tablecode = viewPanel.getTablecode();
		List<Map> maps = viewCfgService.getParseColumnCodeTree(tablecode);
		write(JSONArray.fromObject(maps).toString());
		logger.info("ViewCfgAction getParseColumnCodeTree method run end.");
		return null;
	}

	/**
	 * Description : 获得本菜单下的内容窗口
	 * 
	 * @return
	 * @throws Exception
	 * 
	 */
	@SuppressWarnings("unchecked")
	public String getItemWin() throws Exception
	{
		logger.info("ViewCfgAction getItemWin method run begin.");
		String viewpanelidStr = request.getParameter("viewpanelid");
		List<ViewWin> viewWins = viewCfgService
				.findViewWinByViewPanelIdAndWintype(viewpanelidStr,
						ViewWin.WINTYPE_ITEM);
		Map map = viewParseService.getParseDynamicComboFromList(getUserId(),
				menuId, viewWins);
		write(JSONObject.fromObject(map).toString());
		logger.info("ViewCfgAction getItemWin method run end.");
		return null;
	}

	/**
	 * Description : 新增界面内容项
	 * 
	 * @return
	 * @throws Exception
	 * 
	 */
	@SuppressWarnings("unchecked")
	public String saveOrUpdateViewItem() throws Exception
	{
		logger.info("ViewCfgAction saveOrUpdateViewItem method run begin.");
		Map inputMap = ViewAction.requestParameterMap2Map(request);
		Map objectMap = ViewUtil.ColumnMap2ObjectMapBy_C(inputMap);
		ViewItem viewItem = (ViewItem) ViewUtil.map2Object(objectMap,
				ViewItem.class);
		boolean saveFlag = true;
		if (null != viewItem.getId() && viewItem.getId() > 0)
		{
			saveFlag = false;
		}
		viewCfgService.saveOrUpdateViewItem(viewItem);
		if (saveFlag)
		{
			createLog("addViewItem", "ViewItem id:" + viewItem.getId());
		} else
		{
			createLog("updateViewItem", "ViewItem id:" + viewItem.getId());
		}
		Map result = new HashMap();
		result.put(ViewConstant.JSON_DEAL_SUCESS, true);
		write(JSONObject.fromObject(result).toString());
		logger.info("ViewCfgAction saveOrUpdateViewItem method run end.");
		return null;
	}

	/**
	 * Description : 删除界面内容项
	 * 
	 * @return
	 * @throws Exception
	 * 
	 */
	@SuppressWarnings("unchecked")
	public String deleteViewItem() throws Exception
	{
		logger.info("ViewCfgAction deleteViewItem method run begin.");
		List<Map> mapList = ViewAction.getDataMap(request);
		List<Long> viewItemids = new ArrayList<Long>();
		for (Map map : mapList)
		{
			Object id = map.get("id");
			if (null != id && !"".equals(id.toString())
					&& id.toString().matches("\\d+"))
			{
				viewItemids.add(Long.valueOf(id.toString()));
			}
		}
		viewCfgService.deleteViewItems(viewItemids);
		createLog("deleteViewItem", "ViewItem id:"
				+ ReflectionToStringBuilder.toString(viewItemids));
		Map result = new HashMap();
		result.put(ViewConstant.JSON_DEAL_SUCESS, true);
		write(JSONObject.fromObject(result).toString());
		logger.info("ViewCfgAction deleteViewItem method run end.");
		return null;
	}

	/**
	 * Description : 获得本表格面板下的界面面板自定义过滤条件
	 * 
	 * @return
	 * @throws Exception
	 * 
	 */
	@SuppressWarnings("unchecked")
	public String getViewPanelFilterByGridPanelId() throws Exception
	{
		logger.info("parameter gridPanelId = " + gridPanelId);
		List<ViewPanelFilter> viewPanelFilters = viewCfgService
				.findViewPanelFilterByViewpanelid(gridPanelId);
		List result = new ArrayList();
		for (ViewPanelFilter viewPanelFilter : viewPanelFilters)
		{
			Map map = getMapByViewPanelFilter(viewPanelFilter);
			if (null != map && map.size() > 0)
			{
				result.add(map);
			}
		}
		write(JSONArray.fromObject(result).toString());
		return null;
	}

	@SuppressWarnings("unchecked")
	private Map getMapByViewPanelFilter(ViewPanelFilter viewPanelFilter)
	{
		Map result = new HashMap();
		if (null != viewPanelFilter)
		{
			result = ViewUtil.Object2Map(viewPanelFilter);
			result.put("filter", viewPanelFilter.getFilterMaps());
		}
		return result;
	}

	/**
	 * Description : 新增界面面板自定义过滤条件
	 * 
	 * @return
	 * @throws Exception
	 * 
	 */
	@SuppressWarnings("unchecked")
	public String saveOrUpdateViewPanelFilter() throws Exception
	{
		Map inputMap = ViewAction.requestParameterMap2Map(request);
		String viewpanelid = (String) inputMap.get("viewpanelid");
		inputMap.put("viewpanelid", ViewUtil.getViewControlId(viewpanelid)
				.toString());
		Set keySet = inputMap.keySet();
		ViewPanelFilter viewPanelFilter = new ViewPanelFilter();
		BeanMap beanMap = new BeanMap(viewPanelFilter);
		Map filterMap = new HashMap();
		for (Iterator it = keySet.iterator(); it.hasNext();)
		{
			String key = (String) it.next();
			String value = (String) inputMap.get(key);
			if (key.startsWith(ViewConstant.JSON_GRID_FILTER)
					&& !ViewUtil.isNuLLStr(value))
			{
				filterMap.put(key, value);
				continue;
			}
			if (beanMap.containsKey(key) && !ViewUtil.isNuLLStr(value))
			{
				beanMap.put(key, value);
				continue;
			}
		}
		viewPanelFilter = (ViewPanelFilter) beanMap.getBean();
		viewPanelFilter.setFilter(JSONObject.fromObject(filterMap).toString());
		boolean saveFlag = true;
		if (null != viewPanelFilter.getId() && viewPanelFilter.getId() > 0)
		{
			saveFlag = false;
		}
		viewCfgService.saveOrUpdateViewPanelFilter(viewPanelFilter);
		if (saveFlag)
		{
			createLog("addViewPanelFilter", "ViewPanelFilter id:"
					+ viewPanelFilter.getId());
		} else
		{
			createLog("updateViewPanelFilter", "ViewPanelFilter id:"
					+ viewPanelFilter.getId());
		}
		Map result = new HashMap();
		result.put(ViewConstant.JSON_DEAL_SUCESS, true);
		write(JSONObject.fromObject(result).toString());
		return null;
	}

	/**
	 * Description : 删除界面面板自定义过滤条件
	 * 
	 * @return
	 * @throws Exception
	 * 
	 */
	@SuppressWarnings("unchecked")
	public String deleteViewPanelFilter() throws Exception
	{
		List<Map> mapList = ViewAction.getDataMap(request);
		List<Long> viewPanelFilterids = new ArrayList<Long>();
		for (Map map : mapList)
		{
			Object id = map.get("id");
			if (null != id && !"".equals(id.toString())
					&& id.toString().matches("\\d+"))
			{
				viewPanelFilterids.add(Long.valueOf(id.toString()));
			}
		}
		viewCfgService.deleteViewPanelFilters(viewPanelFilterids);
		createLog("deleteViewPanelFilter", "ViewPanelFilter id:"
				+ ReflectionToStringBuilder.toString(viewPanelFilterids));
		Map result = new HashMap();
		result.put(ViewConstant.JSON_DEAL_SUCESS, true);
		write(JSONObject.fromObject(result).toString());
		return null;
	}

	/**
	 * Description : 获得本表格面板下的界面面板自定义过滤条件输入项信息
	 * 
	 * @return
	 * @throws Exception
	 * 
	 */
	public String getViewPanelFilterInputFormByGridPanelId() throws Exception
	{
		logger.info("parameter gridPanelId = " + gridPanelId);
		String result = viewParseService
				.getParseViewPanelFilterInputFormJsonByPanelid(getUserId(),
						gridPanelId);
		write(result);
		return null;
	}
}
